package cc.ufinity.networkdiagnose.widget;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.net.wifi.ScanResult;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import cc.ufinity.networkdiagnose.manager.Draw;
import cc.ufinity.networkdiagnose.manager.WiFiApInfo;
import cc.ufinity.networkdiagnose.manager.WifiAdmin;
import cc.ufinity.networkdiagnose.view.R;

public class YunYSWidget extends GeneralTestWidget {
	private Activity ctx;
	private WifiAdmin WIFI;
	private List<WiFiApInfo> ZGYDwifis = new ArrayList<WiFiApInfo>();
	private List<WiFiApInfo> ZGDXwifis = new ArrayList<WiFiApInfo>();
	private List<WiFiApInfo> ZGLTwifis = new ArrayList<WiFiApInfo>();
	private List<ScanResult> scanResults = new ArrayList<ScanResult>();
	private ScanResult scanResult;
	private WiFiApInfo MAXZGYDWifi = new WiFiApInfo();
	private WiFiApInfo MAXZGDXWifi = new WiFiApInfo();
	private WiFiApInfo MAXZGLTWifi = new WiFiApInfo();

	private TextView zhongguoYDBSSID;
	private TextView zhongguoYDMAC;
	private TextView zhongguoYDLEVEL;

	private TextView zhongguoDXBSSID;
	private TextView zhongguoDXMAC;
	private TextView zhongguoDXLEVEL;
	private TextView zhongguoLTBSSID;
	private TextView zhongguoLTMAC;
	private TextView zhongguoLTLEVEL;

	private LinearLayout layout;
	private Draw draw;
	public Handler ObjectHandler;
	
	public YunYSWidget(Activity context) {
		super(context);
		// TODO Auto-generated constructor stub
		this.ctx = context;
		inflate(ctx, R.layout.yunys_widget, this);
		WIFI = new WifiAdmin(ctx);
		
		ObjectHandler=new Handler();
		
		MAXZGYDWifi.setLEVEl(-200);
		MAXZGDXWifi.setLEVEl(-200);
		MAXZGLTWifi.setLEVEl(-200);
		layout=(LinearLayout)findViewById(R.id.LineLayout);
		
		Button backButton=(Button)findViewById(R.id.yys_test_btnback);

		backButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				ObjectHandler.removeCallbacks(mTasks);
				 back(ctx);
			}
		});
		init();
	}

	public void init() {
		getScanApInfos();

		SelectZGYDMaxNet();
		SelectZGDXMaxNet();
		SelectZGLTMaxNet();

		zhongguoDXBSSID = (TextView) findViewById(R.id.zhongdianxinBSSID);
		zhongguoDXMAC = (TextView) findViewById(R.id.zhongdianxinMAC);
		zhongguoDXLEVEL = (TextView) findViewById(R.id.zhongguodianxinRSSI);
		zhongguoYDBSSID = (TextView) findViewById(R.id.zhongguoyidongBSSID);
		zhongguoYDMAC = (TextView) findViewById(R.id.zhongguoyidongMAC);
		zhongguoYDLEVEL = (TextView) findViewById(R.id.zhongguoyidongRSSI);
		zhongguoLTBSSID = (TextView) findViewById(R.id.zhongguoliantongBSSID);
		zhongguoLTMAC = (TextView) findViewById(R.id.zhongguoliantongMAC);
		zhongguoLTLEVEL = (TextView) findViewById(R.id.zhongguoliantongRSSI);
		if (MAXZGDXWifi.getLEVEl() != -200) {
			zhongguoDXBSSID.setText("SSID:" + MAXZGDXWifi.getSSID());
			zhongguoDXMAC.setText("MAC:" + MAXZGDXWifi.getBSSID());
			zhongguoDXLEVEL.setText("LEVEL:" + MAXZGDXWifi.getLEVEl());
		} else {
			zhongguoDXBSSID.setText("SSID:");
			zhongguoDXMAC.setText("MAC:");
			zhongguoDXLEVEL.setText("LEVEL:");
		}

		if (MAXZGYDWifi.getLEVEl() != -200) {
			zhongguoYDBSSID.setText("SSID:" + MAXZGYDWifi.getSSID());
			zhongguoYDMAC.setText("MAC:" + MAXZGYDWifi.getBSSID());
			zhongguoYDLEVEL.setText("LEVEL:" + MAXZGYDWifi.getLEVEl());
		} else {
			zhongguoYDBSSID.setText("SSID:");
			zhongguoYDMAC.setText("MAC:");
			zhongguoYDLEVEL.setText("LEVEL:");
		}

		if (MAXZGLTWifi.getLEVEl() != -200) {
			zhongguoLTBSSID.setText("SSID:" + MAXZGLTWifi.getSSID());
			zhongguoLTMAC.setText("MAC:" + MAXZGLTWifi.getBSSID());
			zhongguoLTLEVEL.setText("LEVEL:" + MAXZGLTWifi.getLEVEl());
		} else {
			zhongguoLTBSSID.setText("SSID:");
			zhongguoLTMAC.setText("MAC:");
			zhongguoLTLEVEL.setText("LEVEL:");
		}
		draw=new Draw(ctx,MAXZGYDWifi,MAXZGDXWifi,MAXZGLTWifi);
		 layout.removeAllViews();
		layout.addView(draw);
		 ObjectHandler.postDelayed(mTasks, 2000);//开始刷新
	}

	/**
	 * 这个方法是为了回去不同的运营商的网络的信息 包括，中国电信，中国移动，中国联通
	 */
	public void getScanApInfos() {
		WIFI.startScan();
		scanResults = WIFI.getWifiList();
		for (int i = 0; i < scanResults.size(); i++) {
			WiFiApInfo wifi = new WiFiApInfo();
			scanResult = scanResults.get(i);
			if (scanResult.SSID.startsWith("CMCC")) {
				System.out.println("---------------CMCC---------------");
				wifi.setBSSID(scanResult.BSSID);
				wifi.setAuth(scanResult.capabilities);
				wifi.setLEVEl(scanResult.level);
				wifi.setSSID(scanResult.SSID);
				ZGYDwifis.add(wifi);
			} else if (scanResult.SSID.startsWith("ChinaNet")) {
				System.out.println("---------------ChinaNet---------------");
				wifi.setBSSID(scanResult.BSSID);
				wifi.setAuth(scanResult.capabilities);
				wifi.setLEVEl(scanResult.level);
				wifi.setSSID(scanResult.SSID);
				ZGDXwifis.add(wifi);
			} else if(scanResult.SSID.toLowerCase().startsWith("chinaunicom")||scanResult.SSID.toLowerCase().startsWith("cu")){
				System.out.println("---------------联通---------------");
				wifi.setBSSID(scanResult.BSSID);
				wifi.setAuth(scanResult.capabilities);
				wifi.setLEVEl(scanResult.level);
				wifi.setSSID(scanResult.SSID);
				ZGLTwifis.add(wifi);
			}
		}
	}

	/**
	 * 这个方法是为了获取中国移动的信号最大的AP
	 */
	public void SelectZGYDMaxNet() {
		System.out.println("+++中国移动+++++" + ZGYDwifis.size());
		for (int i = 0; i < ZGYDwifis.size(); i++) {
			MAXZGYDWifi = Compare(ZGYDwifis.get(i), MAXZGYDWifi);
		}
	}
	
	/**
	 * 这个是为了刷新当前的Activity
	 */
	public Runnable mTasks = new Runnable(){
		 public void run(){
			try{
				 init();
			}catch(Exception e){
				e.printStackTrace();
			}
		 }
	};

	/**
	 * 这个方法是为了获取中国电信信号最大的AP
	 */
	public void SelectZGDXMaxNet() {
		System.out.println("+++中国电信+++++" + ZGDXwifis.size());
		for (int i = 0; i < ZGDXwifis.size(); i++) {
			MAXZGDXWifi = Compare(ZGDXwifis.get(i), MAXZGDXWifi);
		}
	}

	/**
	 * 这个方法是为了获取中国联通信号最大的AP
	 */
	public void SelectZGLTMaxNet() {
		System.out.println("+++中国联通+++++" + ZGLTwifis.size());
		for (int i = 0; i < ZGLTwifis.size(); i++) {
			MAXZGLTWifi = Compare(ZGLTwifis.get(i), MAXZGLTWifi);
		}
	}

	/**
	 * 这个方法是为了比较两个AP的信号的大小，返回大的那个AP
	 * 
	 * @param wifiA
	 * @param wifiB
	 * @return
	 */
	public WiFiApInfo Compare(WiFiApInfo wifiA, WiFiApInfo wifiB) {
		System.out.println("+++++++++++A+++++++++++++" + wifiA.getLEVEl()
				+ "\n" + "++++++++++B+++++++" + wifiB.getLEVEl());
		if (wifiA.getLEVEl() >= wifiB.getLEVEl()) {
			System.out.println("++++++++A===========");
			return wifiA;
		} else {
			System.out.println("++++++++B===========");
			return wifiB;
		}
	}
	
	
}